package e_commerce.parser;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.CssSelectorNodeFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import e_commerce.data.Deal;

public class NewEggDailyDealParser {
	private static final BasicNameValuePair QUERY_DAILY_DEAL = new BasicNameValuePair(
			"Name", "DailyDeal");
	private static final String PATH_DAILY_DEAL = "DailyDeal.aspx";
	private static final String NEW_EGG_HOST = "www.newegg.com";
	private static final String URL_ENCODING_CHARSET = "UTF-8";

	private boolean isDebug = true;
	NodeFilter dealBlocksFilter;
	NodeFilter nameFilter;
	NodeFilter priceFilter;
	NodeFilter originalPriceFilter;
	NodeFilter hightlightsFilter;
	NodeFilter categoryFilter;
	public NewEggDailyDealParser() {
		this.dealBlocksFilter = new CssSelectorNodeFilter(
				"div[class=productCells] div[class=itemText] a[title~=Details]");
		this.nameFilter = new CssSelectorNodeFilter("div[class=grpArticle] div[class~=grpDesc] h1");
		
	}

	public void parse() throws ParserException, URISyntaxException,
			ClientProtocolException, IOException {
		List<String> detailURLs = parseDetailURLs();
		for (String dURL : detailURLs) {
			Deal deal = parseDetail(dURL);
			print(deal);
		}
	}

	private Deal parseDetail(String detailURL) throws ClientProtocolException, ParserException, URISyntaxException, IOException {
		String page = retrievePage(detailURL);
		Deal d = new Deal();
		d.setName(parseName(page));
		d.setImageURL(parseImageURL(page));
		return d;
	}

	private String parseImageURL(String page) {
		// TODO Auto-generated method stub
		return null;
	}

	private String parseName(String page) throws ParserException {
		NodeList nl = extractPage(page, nameFilter);
		return nl.size() > 0 ? nl.elementAt(0).toPlainTextString() : null;
	}

	private List<String> parseDetailURLs() throws ParserException,
			URISyntaxException, ClientProtocolException, IOException {
		NameValuePair[] queries = { QUERY_DAILY_DEAL,
				new BasicNameValuePair("Page", "1") };
		String html = retrievePage(NEW_EGG_HOST, PATH_DAILY_DEAL, queries);
		NodeList nList = extractPage(html, dealBlocksFilter);

		List<String> dURLs = new ArrayList<String>(0);
		for (Node n : nList.toNodeArray()) {
			String url = getURL(n);
			if (url != null) {
				if (isDebug) {
					print(url);
				}
				dURLs.add(url);
			}
		}
		return dURLs;
	}


	private NodeList extractPage(String page, NodeFilter filter) throws ParserException {
		Parser p = new Parser(page);
		NodeList nList = p.extractAllNodesThatMatch(filter);
		return nList;
	}

	private void print(Object o) {
		System.out.println(o);
	}

	private String getURL(Node n) {
		if (n instanceof LinkTag) {
			LinkTag l = (LinkTag) n;
			return l.getAttribute("href");
		}
		return null;
	}

	private String retrievePage(String uriStr) throws URISyntaxException,
			ClientProtocolException, IOException {
		return HttpClientUtility.getStrByHttp(uriStr);
	}

	private String retrievePage(String host, String path,
			NameValuePair[] queries) throws URISyntaxException,
			ClientProtocolException, IOException {
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();

		if (queries != null) {
			for (NameValuePair q : queries) {
				qparams.add(q);
			}
		}

		URI uri = URIUtils.createURI("http", host, -1, path, URLEncodedUtils
				.format(qparams, URL_ENCODING_CHARSET), null);
		return HttpClientUtility.getStrByHttp(uri);
	}

	public static void main(String[] args) throws ParserException,
			URISyntaxException, ClientProtocolException, IOException {
		NewEggDailyDealParser p = new NewEggDailyDealParser();
		p.parse();
	}
}
